//
//  JCNumberUtil.swift
//  JCPublicKit
//
//  Created by JasonLee on 2020/5/13.
//

import Foundation

class JCNumberUtil: NSObject {
    /**
    向上取整（四舍五入）
    如: 3.3 四舍五入后为 3
    如: 3.7 四舍五入后为 4
    */
    @objc static func roundUp(_ value: Double) -> Int {
        var tempDouble = value
        tempDouble.round()
        return Int(tempDouble)
    }
    /**
    向下取整
    如: 3.7 double 取整后为 3 int
    */
    @objc static func roundDown(_ value: Double) -> Int {
        var tempDouble = value
        tempDouble.round(.towardZero)
        return Int(tempDouble)
    }
    /**
    如果有小数点，显示带小数点
    如果没有小数点，显示整型
    */
    @objc static func beautifyString(_ value: Double) -> String {
        var tempSelf = value
        tempSelf.round()
        if tempSelf == value {
            return "\(Int(value))"
        } else {
            return "\(value)"
        }
    }
    /**
    保留两位小数的金融数字
    */
    @objc static func beautifyCurrencyString(_ value: Double) -> String {
        var tempSelf = value
        tempSelf.round()
        if tempSelf == value {
            return "\(Int(value))"
        } else {
            let expandedSelf = Int(value * 100)
            let shrinkSelf = Double(expandedSelf) / 100
            return String(format: "%.2f", shrinkSelf)
        }
    }
}

extension Double {
    var jc_roundUp: Int {
        JCNumberUtil.roundUp(self)
    }
    var jc_roundDown: Int {
        JCNumberUtil.roundDown(self)
    }
}

extension Int{
    mutating func jc_increased() {
        self += 1
    }
    mutating func jc_decreased()  {
        if self > 0 {
            self -= 1
        }
    }
}
